<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
  <meta charset="UTF-8">
  <title>添加管理员-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../static/css/font.css">
  <link rel="stylesheet" href="../../../static/css/weadmin.css">
  <script src="../../../static/lib/layui/layui.js" charset="utf-8"></script>
  <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
  <script>
    $(function () {
      $.get({url: "/role/getMsg",success: function (data) {
          $("#role").html(data)
        },async: false})
    })
  </script>
</head>

<body>
<div class="weadmin-body">
  <form class="layui-form">
    <div class="layui-form-item">
      <label for="L_username" class="layui-form-label">
        <span class="we-red">*</span>登录名
      </label>
      <div class="layui-input-inline">
        <input type="text" id="L_username" name="username" lay-verify="required"  autocomplete="off"
               class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label for="L_email" class="layui-form-label">
        <span class="we-red">*</span>手机
      </label>
      <div class="layui-input-inline">
        <input type="text" id="L_phone" name="phone" lay-verify="required|phone" autocomplete="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label for="L_email" class="layui-form-label">
        <span class="we-red">*</span>邮箱
      </label>
      <div class="layui-input-inline">
        <input type="text" id="L_email" name="email" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label for="role" class="layui-form-label">
        <span class="we-red">*</span>角色
      </label>
      <div class="layui-input-inline">
        <select name="role" id="role" th:fragment="roleList">
          <option th:each="adminRole:${session.adminRoles}" th:value="${adminRole.roleId}">[[${adminRole.roleName}]]</option>
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">
      </label>
      <button class="layui-btn add" lay-filter="add" lay-submit="">增加</button>
    </div>
  </form>
</div>
<script>
  layui.extend({
    admin: '{/}../../static/js/admin'
  });
  layui.use(['form', 'admin', 'jquery', 'table', 'layer'], function () {
    var form = layui.form,
            admin = layui.admin,
            $ = layui.jquery,
            table = layui.table,
            layer = layui.layer;


    //监听提交

    form.on('submit(add)', function () {
      var name = $("input[name='username']").val()
      var phone = $("input[name='phone']").val()
      var email = $("input[name='email']").val()
      var roleId = $("option:selected").val()
      var roleName = $("option:selected").html()
      var admin_data = {name: name,phone: phone,email: email,roleId: roleId}
      $.post({
        url: "/admin/add",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(admin_data),
        dataType: "json",
        success: function (data) {
          var length = parent.$("tbody").children("tr").length
          var num = parseInt(parent.$(".fr>span").html())
          $(".fr>span", window.parent.document).html(++num);
          if (length<10) {
            var date = new Date()
            date = dateFormat("YYYY-mm-dd HH:MM:SS", date)
            var add = "<tr data-id='"+data+"'>\n" +
                    "\t\t\t\t\t<td>\n" +
                    "\t\t\t\t\t\t<div class=\"layui-unselect layui-form-checkbox\" lay-skin=\"primary\" data-id='2'>\n" +
                    "\t\t\t\t\t\t\t<i class=\"layui-icon layui-icon-ok\"></i>\n" +
                    "\t\t\t\t\t\t</div>\n" +
                    "\t\t\t\t\t</td>\n" +
                    "\t\t\t\t\t<td>"+data+"</td>\n" +
                    "\t\t\t\t\t<td>"+name+"</td>\n" +
                    "\t\t\t\t\t<td>"+phone+"</td>\n" +
                    "\t\t\t\t\t<td>"+email+"</td>\n" +
                    "\t\t\t\t\t<td>"+roleName+"</td>\n" +
                    "\t\t\t\t\t<td>"+date+"</td>\n" +
                    "\t\t\t\t\t<td class=\"td-status\">\n" +
                    "\t\t\t\t\t<span class=\"layui-btn layui-btn-normal layui-btn-xs\">已启用</span></td>\n" +
                    "\t\t\t\t\t<td class=\"td-manage\">\n" +
                    "\t\t\t\t\t<a onclick=\"member_stop(this,'10001')\" href=\"javascript:;\" title=\"启用\">\n" +
                    "\t\t\t\t\t<i class=\"layui-icon layui-icon-download-circle\"></i>\n" +
                    "\t\t\t\t\t</a>\n" +
                    "\t\t\t\t\t<a title=\"编辑\" onclick=\"WeAdminEdit('编辑','./edit.html', this, 600, 400)\" href=\"javascript:;\">\n" +
                    "\t\t\t\t\t<i class=\"layui-icon layui-icon-edit\"></i></a>\n" +
                    "\t\t\t\t\t<a title=\"删除\" onclick=\"member_del(this,'要删除的id')\" href=\"javascript:;\">\n" +
                    "\t\t\t\t\t<i class=\"layui-icon layui-icon-delete\"></i>\n" +
                    "\t\t\t\t\t</a>\n" +
                    "\t\t\t\t\t</td>\n" +
                    "\t\t\t\t\t</tr>"
            parent.$('#adminList').append(add)
          }
        },
        async: false
      })

      function dateFormat(fmt, date) {
        let ret;
        const opt = {
          "Y+": date.getFullYear().toString(),        // 年
          "m+": (date.getMonth() + 1).toString(),     // 月
          "d+": date.getDate().toString(),            // 日
          "H+": date.getHours().toString(),           // 时
          "M+": date.getMinutes().toString(),         // 分
          "S+": date.getSeconds().toString()          // 秒
          // 有其他格式化字符需求可以继续添加，必须转化成字符串
        };
        for (let k in opt) {
          ret = new RegExp("(" + k + ")").exec(fmt);
          if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
          };
        };
        return fmt;
      }

      layer.alert("增加成功", {
        icon: 6
      }, function () {
        // 获得frame索引
        var index = parent.layer.getFrameIndex(window.name);
        //关闭当前frame
        parent.layer.close(index);
      });
      return false;
    });
  })

</script>
</body>

</html>